SceneEditor.prototype.showOrHidePolyline=function(layerId,state,type){
	var _this=this;
	if('Polylines'==type){
		layer.load(2);
	    _this.ajaxFunc(_this.ctx+"/scene/polyline/getList",{
	    	layerId:layerId
	    }, "json", function(res){
	    	layer.closeAll('loading');
			 if (undefined!=res && 200==res.code) {
				 var list=res.data;
				 if(state==true){
					 _this.globe.addPolylines(list);
				 }else{
					 _this.globe.removePolylines(list);
				 }
			 }
			 else{
			 }
	    });
	}
	else if ('Polyline'==type){
		layer.load(2);
	    _this.ajaxFunc(_this.ctx+"/scene/polyline/getById",{
	    	id:layerId
	    }, "json", function(res){
	    	layer.closeAll('loading');
			 if (undefined!=res && 200==res.code) {
				 var model=res.data; 
				 if(model){
					 if(state==true){
						 _this.globe.addPolyline(model);
					 }else{
						 _this.globe.removePolyline(model);
					 }
				 }
			 }
			 else{
			 }
	    });
	}else{
		layer.alert('未识别图层标识【"'+type+'"】！！', {icon: 1,skin: 'layer-ext-moon' })
	}
} 


/************************************绘制线*********************************************/
/**
 * 开始绘制
 */
SceneEditor.prototype.drawPolyline = function (node) {
	var _this=this;
	if(this.isDraw){
		layer.msg('已经处于绘制状态！！！');
		return;
	}
	_this.isDraw=true;
	_this.globe.drawPolyline({finish:function(positions){
		_this.isDraw=false;
		if(positions && positions.length>=2){
		    var formData={}; 
		    formData['bizLine.id']=VFG.Util.getUuid();
		    formData['bizLine.name']='线';
		    formData['bizLine.layerId']=node.id;
		    formData['bizLine.points']=JSON.stringify(positions);
		    _this.savePolyLine(formData,function(res){
		        if (res!=undefined && 200==res.code) {
		        	layer.msg(res.message);
		        	_this.globe.addPolyline(res.data);
		        	_this.reAsyncLayerByPId(res.data.layerId);
		        }else{
		        	layer.msg(res?res.message:'保持失败！！');
		        }
		    });
		}
	}});
};

/**
 * 保存
 */
SceneEditor.prototype.savePolyLine=function(formData,callback){
	var _this=this;
    layer.load(2);
    _this.ajaxFunc(_this.ctx+"/biz/line/save",formData, "json", function(res){
    	 layer.closeAll('loading');
    	if(callback){
    		callback(res);
    	}
    });
}

/**
 * 操作点
 */
SceneEditor.prototype.operationPolyline = function (screenPos,polylineId,type) {
	var _this=this;
	 var polyline=_this.globe.getById(polylineId,type);
	if(polyline){
		layer.load(2);
		_this._Ajax('get',_this.ctx+"/biz/line/operation",{sceneId:_this.sceneId,polylineId:polylineId},'html',function(e){
			layer.closeAll('loading');   
			var layerIndex= layer.open({
					id:polylineId,
				    type: 1,
				    title: false,
				    shade: 0.5,
				    content:e,
				    resize :false,
				    success: function (layero, dIndex) {
			            $(layero).children('.layui-layer-content').css('overflow', 'hidden');
			            var cartesian3=VFG.Util.getScreenToC3(_this.viewer, screenPos, null);
			        	VFGWin=new Cesium.VFGWin(_this.viewer,{
			            	position:cartesian3,
			            	layero:layero,
			            	index:dIndex,
			            });
			        	$('#toolForBizOperationDrag').click(function () {
			        		layer.close(dIndex);
			        		_this.globe.removePolylineById(polyline.id);
			        		_this.dragPolyline(polyline.id,polyline.name,VFG.Polyline.getPositions(polyline.points));
			        	});	
			        	$('#toolForBizOperationEdit').click(function () {
			        		layer.close(dIndex);
			        		_this.editPolyLine(polylineId,polyline.name);
			        	});	
			        	$('#toolForBizOperationStyle').click(function () {
			        	});	
			        	$('#toolForBizOperationDel').click(function () {
			        		layer.close(dIndex);
			        		_this.deleteLayer({
			        			id:polylineId,
			        			type:type,
			        			name:polyline.name,
			        		})
			        	});	
					},
					end:function(){
					}
		    	});  
		});
	}else{
		layer.alert('信息未知！！', {icon: 1,skin: 'layer-ext-moon' })
	}
}

/**
 * 编辑
 */
SceneEditor.prototype.editPolyLine = function (lineId,name) {
	var _this=this;
	layer.load(2);
	_this._Ajax('get',_this.ctx+"/scene/polyline/edit",{lineId:lineId},'html',function(e){
	   layer.closeAll('loading');   
	   var layerIndex= layer.open({
	    	id:'editPolyLine',
	        title: name || '编辑【'+name+'】点',
	        type: 1,
	        area: '300px',
	        shade:0,
            offset: ['0px', '224px'],	
            fixed:true,
            move:false,
		    skin:'layui-layer  layui-layer-adminRight layui-layer-city',
		    content: e,
		    success: function (layero, dIndex) {
	            $(layero).children('.layui-layer-content').css('overflow', 'hidden');
	        	form.on('submit(submitBizLineForm)', function (data) {
	                var formData={}; 
	                for(var key in data.field){
	                	formData['bizLine.'+key]=data.field[key];
	                }
	                _this.savePolyLine(formData,function(res){
		                if (res!=undefined && 200==res.code) {
		                    layer.msg(res.message);
		                    _this.reAsyncLayerByPId(res.data.layerId);
		                } else {
		                    layer.msg(res.message);
		                }
	                })
	        	    return false;
	        	});
	        	form.render(); 
			},
			end:function(){
			}
    	});  
	});
};

/**
 * 编辑菜单
 */
SceneEditor.prototype.polyLineForMenu=function(entity,type){
	var _this=this;
	_this.operateForMenu(entity,type);
}

//拖住
SceneEditor.prototype.dragPolyline = function (uniqueId,name,points) {
	var _this=this;
	_this.isDrag=true;
	if(!uniqueId && !points){
		console.log('傻冒，错了！');
		return;
	}
	var map=new Map();
	var insertMap=new Map();
	var positions=points;
	var distanceDisplayCondition;
	var entityId=uniqueId;
	var entity=_this.createLine(uniqueId,name,points)
	_this.showPointForLine(positions,map,insertMap) ;
	var cesiumDrag =new Cesium.Drag(_this.viewer,{
		type:'pointForLine',
		primitive:entity,
		LEFT_DOWN:function(e,pick){
        	if(pick && pick.id && pick.id.id && insertMap.has(pick.id.id)){
        		VFG.Util.inset(positions,pick.id.index,pick.id.position._value);
        	}else{ 
        		_this.clearCenterPointForLine(insertMap);
        	}
		},
		MIDDLE_CLICK:function(id,obj){
            if( map.has(id)){
				var point=map.get(id);
				VFG.Util.splice(positions,point.index, 1);
				_this.clearPointForLine(map);
				_this.clearCenterPointForLine(insertMap);
				_this.showPointForLine(positions,map,insertMap) ;
            }
		},
		mouseMove:function(id,e){
			if(map.has(id)){
				var point=map.get(id);
				point.position = new Cesium.CallbackProperty(function () {
                    return e;
                }, false);
				positions[point.index]=e;
			}
			else if(insertMap.has(id)){
				var point=insertMap.get(id);
				point.position = new Cesium.CallbackProperty(function () {
                    return e;
                }, false);
				positions[point.index]=e;
			}
		},
		LEFT_UP:function(e){
			//清中间点位
			_this.clearPointForLine(map);
			_this.clearCenterPointForLine(insertMap);
			_this.showPointForLine(positions,map,insertMap) ;
		},
		end:function(){
			_this.isDrag=false;
			cesiumDrag=null;
		    var formData={}; 
		    formData['bizLine.id']=entityId;
		    formData['bizLine.points']=JSON.stringify(VFG.Util.c3sToLnLas(_this.viewer,positions));
		    _this.savePolyLine(formData,function(res){
		        if (res!=undefined && 200==res.code) {
		        	layer.msg(res.message)
					_this.clearPointForLine(map);
					_this.clearCenterPointForLine(insertMap);
					_this.viewer.entities.removeById(uniqueId);
		        	_this.globe.addPolyline(res.data);
		        }else{
		        	layer.msg(res?res.message:'保持失败！！！')
		        }
		    });
		}
	});
}
SceneEditor.prototype.showPointForLine = function (positions,map,insertMap) {
	var _this=this;
	for(var i=0;i<positions.length;i++){
		var uuid=VFG.Util.getUuid();
		var point=_this.addPointForLine(uuid,i,i,positions[i]);
		map.set(uuid,point);
		if(i+1<positions.length){
			var uuid=VFG.Util.getUuid();
			var centePoint=VFG.Util.getCenterPoint(positions[i],positions[i+1]);
			var point=_this.insertCenterPointForLine(uuid,i+1,i+1,centePoint);
			insertMap.set(uuid,point);
		}
	}
}
/**
 * 清除辅助点
 */
SceneEditor.prototype.clearPointForLine = function (map) {
	var _this=this;
	map.forEach(function(value,key){
		VFG.Util.removeEntityById(_this.viewer,key)
	});
	map.clear();
}
/**
 * 清除插入的辅助点
 */
SceneEditor.prototype.clearCenterPointForLine = function (map) {
	var _this=this;
	map.forEach(function(value,key){
		VFG.Util.removeEntityById(_this.viewer,key)
	});
	map.clear();
}

SceneEditor.prototype.addPointForLine = function (id,name,index,position) {
	var ops={
	        id: id,
	        name:name,
	        type:'pointForLine',
	        show:true,
	        index:index,
	        position:position,
	        point:new Cesium.PointGraphics ( {
	            show : true,
	            pixelSize : 5,
	            heightReference :Cesium.HeightReference.NONE,
	            color :Cesium.Color.YELLOW,
	            outlineColor :Cesium.Color.RED,
	            outlineWidth :1,
	            eyeOffset: new Cesium.Cartesian3(0, 0, -10)
	        } )
	    }
	return this.viewer.entities.add(new Cesium.Entity(ops));
}
SceneEditor.prototype.insertCenterPointForLine = function (id,name,index,position) {
	var ops={
	        id: id,
	        name:name,
	        type:'pointForLine',
	        show:true,
	        index:index,
	        position:position,
	        point:new Cesium.PointGraphics ( {
	            show : true,
	            pixelSize : 5,
	            heightReference :Cesium.HeightReference.NONE,
	            color :Cesium.Color.CORNFLOWERBLUE,
	            outlineColor :Cesium.Color.RED,
	            outlineWidth :1,
	            eyeOffset: new Cesium.Cartesian3(0, 0, -10)
	        } )
	    }
	return this.viewer.entities.add(new Cesium.Entity(ops));
}

SceneEditor.prototype.createLine = function (id,name,positions) {
	var _this=this;
	_this.viewer.entities.removeById(id);
	return _this.viewer.entities.add({
		id:id,
        name: name?name:"",
        polyline: {
            positions: new Cesium.CallbackProperty(function () {
	            return positions;
	        }, false),
            width: 3.0,
            clampToGround: true,
            material:  Cesium.Color.RED,
        }
    });
}

/**
 * 设置
 */
SceneEditor.prototype.editPolylineLayer=function(node){
	var _this=this;
	 layer.load(2);
	 _this._Ajax('get',_this.ctx+"/scene/polyline/setting",{id:node.id},'html',function(e){
		layer.closeAll();
		layer.open({
		  id:'editPolyLineLayer',
          type: 1,
          anim:5,
          title:'编辑【'+node.name+'】',
          area: '300px',
          content:e,
          shade:0,
          offset: ['0px', '224px'],	
          fixed:true,
          move:false,
		  skin:'layui-layer  layui-layer-adminRight layui-layer-city',
          success: function (layero, dIndex) {
        	   	$(layero).children('.layui-layer-content').css('overflow', 'hidden');
	           	element.on('tab(lineStyleTabFilter)', function(data){
	           		 curStyleTabIndex=data.index;
	           	}); 
	           	
	      	    treeSelect.render({
	    	        elem: '#parentId',
	    	        data: _this.ctx+'/scene/layer/getTree?sceneId='+_this.sceneId+'&type=Layers',
	    	        type: 'get',
	    	        placeholder: '请选择',
	    	        search: true,
	    	        click: function(d){
	    	        },
	    	        success: function (d) {
	    	        	if(node){
	    	        		treeSelect.checkNode('parentId', node.parentId);
	    	        	}
	    	        }
	    	    });
	           	
	           	
               	var curStyleTabIndex=0;
	           	form.on('select(lineType)', function(data){
	          		if("0"==data.value){
	          			_this.findPolylineStyle(curStyleTabIndex,node);
	          		}
	          		else if("1"==data.value){
	          		}
	          		form.render();
	          	});
	           	form.on('select(lineHoverType)', function(data){
	           		if("0"==data.value){
	           			_this.findPolylineStyle(curStyleTabIndex,node);
	           		}
	           		else if("1"==data.value){
	           		}
	           		form.render();
	           	});
	           	form.on('select(lineSelectType)', function(data){
	           		if("0"==data.value){
	           			_this.findPolylineStyle(curStyleTabIndex,node);
	           		}
	           		else if("1"==data.value){
	           		}
	           		form.render();
	           });         	 
               form.on('submit(submitBizLayerForm)', function (data) {
  	        	    layer.load(2);
  	                var formData={}; 
  	                for(var key in data.field){
  	                	formData['bizSceneLayer.'+key]=data.field[key];
  	                }
  	                _this.ajaxFunc(_this.ctx+"/scene/layer/save",formData, "json", function(res){
  		                layer.closeAll('loading');
  		                if (200==res.code) {
  		                	 layer.msg(res.message, {icon: 1});
  		                	_this.reAsyncLayerByPId(res.data.layerId);
  		                } else {
  		                    layer.msg(res.message, {icon: 2});
  		                }
  	                });
  	        	    return false;
               });
               form.render();
			},
			end:function(){
			}
		});	
	});
}

SceneEditor.prototype.findPolylineStyle=function(curTabIndex,node){
	var _this=this;
	 layer.load(2);
	var id;
 	if(curTabIndex==0) id=node.defaultStyleId;
	else if(curTabIndex==1) id=node.hoverStyleId;
	else if(curTabIndex==2) id=node.selectedStyleId;
    _this.ajaxFunc(_this.ctx+"/scene/polyline/findStyleById",{id:id}, "json", function(res){
    	layer.closeAll('loading');
        if (res.code == 200) {
        	_this.addOrEditPolylineStyle(curTabIndex,node,res.data);
        }else{
        	_this.addOrEditPolylineStyle(curTabIndex,node,null);
        }
    });	
}

SceneEditor.prototype.addOrEditPolylineStyle=function (curTabIndex,node,data){
	var _this=this;
	var text;
	if(curTabIndex==0) text="默认";
	else if(curTabIndex==1) text="悬浮";
	else if(curTabIndex==2) text="选中";
	layer.open({
		id:'addOrEditPolylineStyle',
        type: 1,
        anim:5,
        title:'【'+node.name+'】-->【'+text+'】样式设置',
        area: '350px',
        content:$('#modelBizStyleLineScript').html(),
        shade:0,
        offset: ['0px', '224px'],	
        fixed:true,
        move:false,
		skin:'layui-layer  layui-layer-adminRight layui-layer-city',
        success: function (layero, dIndex) {
        	$(layero).children('.layui-layer-content').css('overflow', 'hidden');
           	form.val('modelBizStyleLineForm', data);  // 回显数据
        	colorpicker.render({//间隔颜色
        		elem: '#color',
        		color: (data&&data.color)?data.color:null,
        		format: 'rgb',
        		predefine: true,
        		alpha: true,
        		done: function(color) {
        			$('#color-input').val(color); //向隐藏域赋值
        			color || this.change(color); //清空时执行 change
        		},
        		change: function(color) {}
        	});
        	
        	colorpicker.render({//间隔颜色
        		elem: '#outlineColor',
        		color: (data&&data.outlineColor)?data.outlineColor:null,
        		format: 'rgb',
        		predefine: true,
        		alpha: true,
        		done: function(color) {
        			$('#outlineColor-input').val(color); //向隐藏域赋值
        			color || this.change(color); //清空时执行 change
        		},
        		change: function(color) {}
        	});	
        	
        	colorpicker.render({//间隔颜色
        		elem: '#gapColor',
        		color: (data&&data.gapColor)?data.gapColor:null,
        		format: 'rgb',
        		predefine: true,
        		alpha: true,
        		done: function(color) {
        			$('#gapColor-input').val(color); //向隐藏域赋值
        			color || this.change(color); //清空时执行 change
        		},
        		change: function(color) {}
        	});	
        	
			$('#imageUrl-select-btn').click(function () {
				_this.selectIcon('lineEffect',function(data){
		        	$("#imageUrl-img").attr("src",_this.ctx+"/"+data.url);
		        	$("#imageUrl-input").val(data.url);
				});
				return false;
			});
        	
	    	//显示图片
	    	if(data&& data.imageUrl){
	    		$("#imageUrl-img").attr("src",_this.ctx+"/"+data.imageUrl);
	    	}
			
        	form.on('select(materialType)', function(data){
        		if("outline"==data.value){
        			showForOutline();
        		}
        		else if("dash"==data.value){
        			showForDash();
        		} 
        		else if("glow"==data.value){
        			showForGlow();
        		} 
        		else if((data.value=='od')){
        			showForOd();
        		} 
        		else{
        			hiddenTr();
        		}
        		form.render();
        	})
        	
        	if(data&& data.materialType=='dash')
        		showForDash();
        	else if(data&& data.materialType=='outline')	
        		showForOutline();
        	else if(data&& data.materialType=='glow')
        		showForGlow();
        	else if(data&& (data.materialType=='od'))
        		showForOd();
        	else		
        		hiddenTr();	
		    form.on('submit(modelSubmitBizStyleLineForm)', function (data) {
		        layer.load(2);
		        var formData={}; 
		        for(var key in data.field){
		        	formData['bizStyleLine.'+key]=data.field[key];
		        }
	        	if(curTabIndex==0)  formData['bizStyleLine.name']=node.name+"【默认】";
	        	else if(curTabIndex==1)  formData['bizStyleLine.name']=node.name+"【悬浮】";
	        	else if(curTabIndex==2)  formData['bizStyleLine.name']=node.name+"【选中】";
		        formData['bizStyleLine.code']=node.code;
	        	if(curTabIndex==0) formData['bizStyleLine.id']=node.defaultStyleId;
	        	else if(curTabIndex==1) formData['bizStyleLine.id']=node.hoverStyleId;
	        	else if(curTabIndex==2) formData['bizStyleLine.id']=node.selectedStyleId;
		        console.log(formData);
			    _this.ajaxFunc(_this.ctx+"/scene/polyline/saveStyle",formData, "json", function(res){
			    	layer.closeAll('loading');
	                if (res.code == 200) {
	                    layer.msg(res.message, {icon: 1});
	                    _this.globe.updateStyle(res.data);
	                } else {
	                    layer.msg(res.message, {icon: 2});
	                }
			    });
		        return false;
		    });
		    
			//外边框材质
			function showForOutline(){
				$('#outlineColor-tr').css('display','');
				$('#outlineWidth-tr').css('display','');
				$('#glowPower-tr').css('display','none');
				$('#gapColor-tr').css('display','none');
				$('#dashLength-tr').css('display','none');
				$('#dashPattern-tr').css('display','none');
				$('#interval-tr').css('display','none');
				
			}
			//虚线材质
			function showForDash(){
				$('#gapColor-tr').css('display','');
				$('#dashLength-tr').css('display','');
				$('#dashPattern-tr').css('display','');
				$('#outlineColor-tr').css('display','none');
				$('#outlineWidth-tr').css('display','none');
				$('#glowPower-tr').css('display','none');
				$('#interval-tr').css('display','none');
				
			}
			//发光材质
			function showForGlow(){
				$('#glowPower-tr').css('display','');
				$('#outlineColor-tr').css('display','none');
				$('#outlineWidth-tr').css('display','none');
				$('#gapColor-tr').css('display','none');
				$('#dashLength-tr').css('display','none');
				$('#dashPattern-tr').css('display','none');
				$('#interval-tr').css('display','none');
				
			}
			//od
			function showForOd(){
				$('#interval-tr').css('display','');
				$('#direction-tr').css('display','');
				$('#odMapUrl-tr').css('display','');
				$('#animation-tr').css('display','');
				
				$('#outlineColor-tr').css('display','none');
				$('#outlineWidth-tr').css('display','none');
				$('#gapColor-tr').css('display','none');
				$('#glowPower-tr').css('display','none');
				$('#dashLength-tr').css('display','none');
				$('#dashPattern-tr').css('display','none');
			}
			function hiddenTr(){
				$('#outlineColor-tr').css('display','none');
				$('#outlineWidth-tr').css('display','none');
				$('#glowPower-tr').css('display','none');
				$('#gapColor-tr').css('display','none');
				$('#dashLength-tr').css('display','none');
				$('#dashPattern-tr').css('display','none');
				$('#interval-tr').css('display','none');
				$('#odMapUrl-tr').css('display','none');
				$('#animation-tr').css('display','none');
			}
			
        	$('#minVisualHeightBtn').click(function () {
        		 $("#distanceDisplayConditionX").val(_this.getVisualHeight());
        		 return false;
        	});	
        	$('#maxVisualHeightBtn').click(function () {
	    		 $("#distanceDisplayConditionY").val(_this.getVisualHeight());
	    		 return false;
        	});
		    form.render();            
		},
		end:function(){
		}
	});
}
